package com.zimmur.service.impl.account;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.dao.account.IMobileSendtimersBlackipDao;
import com.zimmur.dao.account.IMobileSendtimersDao;
import com.zimmur.model.account.MobileSendtimers;
import com.zimmur.model.account.MobileSendtimersBlackip;
import com.zimmur.model.account.MobileSendtimersExample;
import com.zimmur.service.account.IMobileSendTimersService;
import com.zimmur.tools.DateUtils;

@Service
public class MobileSendTimersServiceImpl implements IMobileSendTimersService {
	private Logger log = LoggerFactory.getLogger(MobileSendTimersServiceImpl.class);
	
	@Resource
	private IMobileSendtimersDao mobileSendTimersDao;
	@Resource
	private IMobileSendtimersBlackipDao mobileSendBlackIpDao;
	
	@Override
	public boolean checkMobileSendTimers(String mobileNo,String ip, int limitTimers) {
		
		try {
			//检查是否IP黑名单
			if(!"".equals(ip)){
			  MobileSendtimersBlackip blackModel =	mobileSendBlackIpDao.selectByPrimaryKey(ip);
			  if(blackModel != null && blackModel.getIsblack() == 1){
				  return false;
			  }
			}
			
			int nowTime = DateUtils.getNowUnixTime();
			MobileSendtimers model = mobileSendTimersDao.selectByPrimaryKey(mobileNo);
			if(model == null){
				insertNewMobileNO(mobileNo,ip);
				return true;
			}

			if(model.getIsblack()==1){
				return false;
			}
			//显示每分钟发送
			int lastOneTime = 60;
			int spanTime = nowTime - model.getLastsenddate();
			if(spanTime < lastOneTime){
				return false;
			}
			
			int firstAndLastSpan = (model.getLastsenddate() - model.getFirstsenddate() )/60;
			if(firstAndLastSpan<=20 && model.getSendtimers()>=limitTimers){
				model.setIsblack(1);
				updateTimers(model);
				return false;
			}else if(firstAndLastSpan > 60 && model.getIsblack()!=1){
				model.setFirstsenddate(nowTime);
				model.setIpaddr(ip);
				model.setLastsenddate(nowTime);
				model.setSendtimers(1);
				updateTimers(model);
				return true;
			}
			
			
			if(!"".equals(ip)){
				
				int checkTime = nowTime - (60*60); //1小时
				MobileSendtimersExample example = new MobileSendtimersExample();
				example.or().andIpaddrEqualTo(ip).andFirstsenddateGreaterThan(checkTime);
				int countTimers = mobileSendTimersDao.countByExample(example);
				if(countTimers>=10){
					insertBlackIp(ip);
					return false;
				}
			}
			
			model.setIpaddr(ip);
			model.setLastsenddate(nowTime);
			model.setSendtimers(model.getSendtimers()+1);
			updateTimers(model);
			return true;
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return false;
	}
	/**
	 * 添加IP黑名单
	 * @param ip
	 * @return
	 */
	private boolean insertBlackIp(String ip){
		boolean bAdd = false;
		MobileSendtimersBlackip model = mobileSendBlackIpDao.selectByPrimaryKey(ip);
		if(model == null){
			bAdd = true;
			model = new MobileSendtimersBlackip();
			model.setIpaddr(ip);
		}
		model.setIsblack(1);
		model.setCreatedate(DateUtils.getNowUnixTime());
		if(bAdd){
			return mobileSendBlackIpDao.insertSelective(model)>0?true:false;
		}else{
			return mobileSendBlackIpDao.updateByPrimaryKeySelective(model)>0?true:false;
		}
		
	}
	private boolean insertNewMobileNO(String mobileNo,String ip){
		MobileSendtimers model = new MobileSendtimers();
		model.setMobileno(mobileNo);
		model.setIpaddr(ip);
		model.setFirstsenddate(DateUtils.getNowUnixTime());
		model.setLastsenddate(DateUtils.getNowUnixTime());
		model.setIsblack(0);
		return mobileSendTimersDao.insertSelective(model)>0?true:false;
	}
	
	private boolean updateTimers(MobileSendtimers model){
		return mobileSendTimersDao.updateByPrimaryKey(model)>0?true:false;
	}
}
